<template>
  <el-container class="layout">
    <el-header class="header">
      <div class="header-content">
        <div class="brand">
          <div class="logo">
            <el-icon size="24" color="#2e7d32"><School /></el-icon>
          </div>
          <div class="brand-text">
            <h1>中山大学研究生院</h1>
            <p>校企对接平台</p>
          </div>
        </div>
        <el-menu mode="horizontal" :ellipsis="false" router class="main-menu" :default-active="currentRoute">
          <el-menu-item index="/">首页</el-menu-item>
          <el-menu-item index="/projects">课题库</el-menu-item>
          <el-menu-item v-if="userStore.canPublishProject" index="/publish">发布课题</el-menu-item>
          <el-menu-item v-if="userStore.canApplyProject" index="/applications">我的申请</el-menu-item>
          <el-menu-item v-if="userStore.canManageSystem" index="/admin">管理端</el-menu-item>
          <el-menu-item v-if="userStore.canManageSystem" index="/statistics">数据统计</el-menu-item>
          <el-menu-item index="/form-demo">组件演示</el-menu-item>
        </el-menu>
        <div class="user-info">
          <el-dropdown @command="handleUserCommand">
            <div class="user-profile">
              <el-avatar :size="32" :src="userStore.currentUser?.avatar || defaultAvatar" />
              <span class="username">{{ userStore.currentUser?.name || '用户' }}</span>
              <el-icon><ArrowDown /></el-icon>
            </div>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="profile">
                  <el-icon><User /></el-icon>
                  个人中心
                </el-dropdown-item>
                <el-dropdown-item command="settings">
                  <el-icon><Setting /></el-icon>
                  账户设置
                </el-dropdown-item>
                <el-dropdown-item divided command="logout">
                  <el-icon><SwitchButton /></el-icon>
                  退出登录
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
    </el-header>
    
    <el-container class="main-container">
      <el-aside width="240px" class="aside">
        <div class="sidebar-header">
          <h3>功能导航</h3>
          <div class="user-role">
            <el-tag :type="getRoleTagType(userStore.userRole)" size="small">
              {{ getRoleText(userStore.userRole) }}
            </el-tag>
          </div>
        </div>
        <el-menu router :default-active="currentRoute" class="sidebar-menu">
          <el-menu-item index="/">
            <el-icon><House /></el-icon>
            <span>平台介绍</span>
          </el-menu-item>
          <el-menu-item index="/projects">
            <el-icon><Document /></el-icon>
            <span>项目浏览</span>
          </el-menu-item>
          <el-menu-item v-if="userStore.canPublishProject" index="/publish">
            <el-icon><Plus /></el-icon>
            <span>发布课题</span>
          </el-menu-item>
          <el-menu-item v-if="userStore.canApplyProject" index="/applications">
            <el-icon><List /></el-icon>
            <span>申请记录</span>
          </el-menu-item>
          <el-menu-item v-if="userStore.canManageSystem" index="/admin">
            <el-icon><Setting /></el-icon>
            <span>匹配管理</span>
          </el-menu-item>
          <el-menu-item v-if="userStore.canManageSystem" index="/statistics">
            <el-icon><TrendCharts /></el-icon>
            <span>数据可视化</span>
          </el-menu-item>
          <el-menu-item index="/form-demo">
            <el-icon><Grid /></el-icon>
            <span>组件演示</span>
          </el-menu-item>
          <el-menu-item index="/about">
            <el-icon><InfoFilled /></el-icon>
            <span>关于平台</span>
          </el-menu-item>
        </el-menu>
      </el-aside>
      
      <el-main class="main">
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { 
  School, House, Document, Plus, List, Setting, TrendCharts, 
  User, ArrowDown, SwitchButton, InfoFilled, Grid
} from '@element-plus/icons-vue'
import { ElMessageBox, ElMessage } from 'element-plus'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

const defaultAvatar = 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png'

// 计算当前路由路径，用于设置导航栏激活状态
const currentRoute = computed(() => route.path)

const getRoleTagType = (role: string | null) => {
  const typeMap: Record<string, string> = {
    enterprise: 'success',
    supervisor: 'primary',
    student: 'warning',
    admin: 'danger'
  }
  return typeMap[role || ''] || 'info'
}

const getRoleText = (role: string | null) => {
  const textMap: Record<string, string> = {
    enterprise: '企业用户',
    supervisor: '导师用户',
    student: '学生用户',
    admin: '管理员'
  }
  return textMap[role || ''] || '未知用户'
}

const handleUserCommand = async (command: string) => {
  switch (command) {
    case 'profile':
      ElMessage.info('个人中心功能开发中...')
      break
    case 'settings':
      ElMessage.info('账户设置功能开发中...')
      break
    case 'logout':
      try {
        await ElMessageBox.confirm(
          '确定要退出登录吗？',
          '退出确认',
          {
            confirmButtonText: '确定退出',
            cancelButtonText: '取消',
            type: 'warning'
          }
        )
        
        userStore.logout()
        ElMessage.success('已退出登录')
        router.push('/login')
      } catch {
        // 用户取消
      }
      break
  }
}
</script>

<style scoped>
.layout {
  min-height: 100vh;
  background: linear-gradient(135deg, #f8f9fa 0%, #e8f5e8 100%);
}

.header {
  background: linear-gradient(135deg, #2e7d32 0%, #4caf50 100%);
  color: white;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  padding: 0;
  height: 80px;
}

.header-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 100%;
  padding: 0 24px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 16px;
}

.logo {
  background: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  width: 48px;
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.brand-text h1 {
  font-size: 20px;
  font-weight: 600;
  margin: 0;
  line-height: 1.2;
}

.brand-text p {
  font-size: 14px;
  margin: 0;
  opacity: 0.9;
}

.main-menu {
  background: transparent;
  border: none;
}

.main-menu .el-menu-item {
  color: white;
  font-weight: 500;
}

.main-menu .el-menu-item:hover {
  background-color: rgba(255, 255, 255, 0.1);
  color: white;
}

.main-menu .el-menu-item.is-active {
  background-color: rgba(255, 255, 255, 0.2);
  color: white;
  border-bottom: 3px solid white;
}

.user-info {
  display: flex;
  align-items: center;
}

.user-profile {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 20px;
  background: rgba(255, 255, 255, 0.1);
  transition: all 0.3s ease;
}

.user-profile:hover {
  background: rgba(255, 255, 255, 0.2);
}

.username {
  font-size: 14px;
  font-weight: 500;
  color: white;
}

.main-container {
  min-height: calc(100vh - 80px);
}

.aside {
  background: white;
  border-right: 1px solid var(--border-color);
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.05);
}

.sidebar-header {
  padding: 20px 16px 16px;
  border-bottom: 1px solid var(--border-color);
  background: var(--background-secondary);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.sidebar-header h3 {
  color: var(--primary-color);
  font-size: 16px;
  font-weight: 600;
  margin: 0;
}

.user-role {
  display: flex;
  align-items: center;
}

.sidebar-menu {
  border: none;
  padding: 16px 0;
}

.sidebar-menu .el-menu-item {
  height: 48px;
  line-height: 48px;
  margin: 4px 16px;
  border-radius: 8px;
}

.sidebar-menu .el-menu-item:hover {
  background-color: rgba(46, 125, 50, 0.1);
}

.sidebar-menu .el-menu-item.is-active {
  background-color: var(--primary-color);
  color: white;
}

.sidebar-menu .el-menu-item .el-icon {
  margin-right: 12px;
  font-size: 18px;
}

.main {
  padding: 24px;
  background: transparent;
}

.el-dropdown-menu .el-dropdown-item .el-icon {
  margin-right: 8px;
}
</style>


